package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    OrderDao orderDao;

    @Autowired
    MemberDao memberDao;

    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        //获取当前日期（今天）
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获取本周一的日期
        String thisWeekMonday =  DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本周最后一天的日期
        String thisWeekSunday = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());
        //获取本月第一天
        String thisMonthFirstDay = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //获取本月最后一天
        String thisMonthLastDay = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());
        //***********************会员相关统计***********************************
        //今日新增会员数
        Integer todayNewMember = memberDao.getTodayNewMember(today);
        //总会员数
        Integer totalMember = memberDao.getTotalMember();
        // 本周新增会员数
        Integer thisWeekNewMember = memberDao.getThisNewMember(thisWeekMonday);
        // 本月新增会员数
        Integer thisMonthNewMember = memberDao.getThisNewMember(thisMonthFirstDay);
        //************************预约订单相关统计*********************************
        //今日预约数
        Integer todayOrderNumber = orderDao.getTodayOrderNumber(today);
        // 今日到诊数
        Integer todayVisitsNumber = orderDao.getTodayVisitsNumber(today);
        // 本周预约数
        Map<String,String> dayMap = new HashMap<>();
        dayMap.put("begin",thisWeekMonday);
        dayMap.put("end",thisWeekSunday);
        Integer thisWeekOrderNumber = orderDao.getThisOrderNumber(dayMap);
        // 本周到诊数
        Integer thisWeekVisitsNumber = orderDao.getThisVisitsNumber(dayMap);
        // 本月预约数
        dayMap.put("begin",thisMonthFirstDay);
        dayMap.put("end",thisMonthLastDay);
        Integer thisMonthOrderNumber = orderDao.getThisOrderNumber(dayMap);
        // 本月到诊数
        Integer thisMonthVisitsNumber = orderDao.getThisVisitsNumber(dayMap);

        //热门套餐
        List<Map<String,Object>> hotSetmeal = orderDao.getHotSetmeal();

        //构造Map
        Map<String, Object> map = new HashMap<>();
        map.put("reportDate",today);//String
        map.put("todayNewMember",todayNewMember);//Integer
        map.put("totalMember",totalMember);//Integer
        map.put("thisWeekNewMember",thisWeekNewMember);//Integer
        map.put("thisMonthNewMember",thisMonthNewMember);//Integer
        map.put("todayOrderNumber",todayOrderNumber);//Integer
        map.put("todayVisitsNumber",todayVisitsNumber);//Integer
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);//Integer
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);//Integer
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);//Integer
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);//Integer
        map.put("hotSetmeal",hotSetmeal);//List<Map<String,Object>>
        return map;
    }
}
